/*
 * main_dll.h
 *
 *  Created on: 21.12.2012
 *      Author: aska
 */

#ifndef MAIN_DLL_H_
#define MAIN_DLL_H_
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include "hopper_ev2000_dll.h"
#include "ftdi2xx.h"

extern HINSTANCE hopper_dll_handle;
bool hopper_is_created;
class hopper_device {
private:
	// variables to store serial port data
	DCB dcb_curr, dcb_prev;	// serial port structures for current and previous states
	COMMTIMEOUTS CommTimeOuts_curr, CommTimeOuts_prev;
	//
	int read_buff_after_writing;	// 1 - to read all data from serial after writing
public:
	int baud;	// Serial port baudrate
	HANDLE hPort;	// Serial port file hadle
	char ser_port_name[32];	// Serial port name
	int ser_port_No;
	int serial_is_opened;
	FT_HANDLE ft_handle;	// handler for ftdi device
	FT_STATUS ft_status;	// variable to store returned value from ftdi functions
	unsigned short ft_LocId;
	HMODULE h_d2xx_dll;
	int d2xx_dll_is_loaded;	// 1 - dll is loaded, 0 - dll not loaded
	int d2xx_opened;
	int ft_handle_opened;
	int dev_interface_mode; // 1 - VCP mode, 2 - d2xx mode
	unsigned char tries_n;	// How many times to retry to perform command
	char log_file_name[255];	// Log file name
	char log_file_path[255];
	FILE * log_file_ptr;	// Log file pointer
	int log_file_year_day_prev;		// Log file previous day since January 1, for logging one file per day
	int log_file_year_day_pres;		// Log file current day since January 1
	int log_add_str(char * log_str);	// Function to write log string. Adds data in the beginning an and CR in the end of string.
	char Ser_nr[8][6];// = new byte[8, 6];
	char buf_RxTx_arr[64];// = new byte[64];
	char Buf_Rx[64];// = new byte[64];
	char Buf_Tx[64];// = new byte[64];
	int get_ser_No(char * vpPort_name);
	int open_ser();
	int open_d2xx_dll(void);
	int open_d2xx(void);
	int device_write(char lpBuffer[], int dwBytesToWrite, int * lpdwBytesWritten);
	int device_read (char lpBuffer[], int dwBytesToRead , int * lpdwBytesReturned);
	int read_buf(char * buf, int * n_recv, int size);
	int purge_buf(void);
	int port_close(void);
	int close_d2xx(void);
	char cctalk_checksum(char * vp_inp_arr, char n_inp);
	int cctalk_write_n_bytes(char * vp_byte_arr, char * vp_byte_arr_out, char n);
	int cctalk_send_simple_pool(char addr);
	int cctalk_address_pool(char * addr);
	int cctalk_request_comms_revision(char addr, char vp_byte_arr[]);
	int cctalk_request_comms_status_variables(char addr, char vp_byte_arr[]);
	int cctalk_clear_comms_variables(char addr);
	int cctalk_request_manufacturer_id(char addr, char vp_byte_arr[]);
	int cctalk_request_equipment_category_id(char addr, char vp_byte_arr[]);
	int cctalk_request_product_code(char addr, char vp_byte_arr[]);
	int cctalk_request_software_revision(char addr, char vp_byte_arr[]);
	int cctalk_request_data_storage_availability(char addr, char vp_byte_arr[]);
	int cctalk_request_build_code(char addr, char vp_byte_arr[]);
	int cctalk_request_address_mode(char addr, char vp_byte_arr[]);
	int cctalk_enter_PIN_number(char addr, char vp_PIN_number[]);
	int cctalk_enter_new_PIN_number(char addr, char vp_PIN_number[]);
	int cctalk_modify_variable_settings(char addr, char vp_variable_settings[]);
	int cctalk_request_variable_settings(char addr, char vp_byte_arr[]);
	int cctalk_test_hopper(char addr, char vp_byte_arr[]);
	int cctalk_hopper_status(char addr, char vp_byte_arr[]);
	int cctalk_emergency_stop(char addr, char vp_byte_arr[]);
	int cctalk_empty_hopper(char addr, char vp_byte_arr[], int coins_to_put);
	int cctalk_request_payout_hi_lo_status(char addr, char vp_byte_arr[]);
	//int cctalk_disable_hopper(char addr);
	int cctalk_pump_RNG(char addr, char vp_RNG[]);
	int cctalk_request_cipher_key(char addr, char vp_byte_arr[]);
	int cctalk_dispense_coins(char addr, char vp_byte_arr[], int coins_to_put);
	int cctalk_request_hopper_dispense_count(char addr, char vp_byte_arr[]);
	int cctalk_read_data_block(char addr, char data_block_nr, char vp_byte_arr[]);
	int cctalk_write_data_block(char addr, char data_block_nr, char nr_of_bytes,  char vp_data[]);
	int cctalk_request_serial_nr(char addr, char * vp_byte_arr);
	int cctalk_reset_device(char addr);
	int cctalk_enable_hopper(char addr);
	int cctalk_disable_hopper(char addr);
	//int hopper_prepare_addr(char addr);
	//int hopper_prepare(char * vp_Port_name);
	hopper_device(void);
	~hopper_device(void);
};

#endif /* MAIN_DLL_H_ */
